<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Mtaikhoan extends CI_Model {

    private $matk;
    private $tenkh;
    private $tentk;
    private $email;
    private $matkhau;
    private $loaitk;
    private $is_login;
    private $ngaysinh;
    private $gioitinh;
private $diachi;
    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->OnCreate();
    }

    function OnCreate() {
        $this->matk = '';
        $this->tentk = '';
        $this->email = '';
        $this->matkhau = '';
        $this->loaitk = '3';
        $this->is_login = FALSE;
        $this->ngaysinh ='';
        $this->gioitinh='';
        $this->tenkh='';
		$this->diachi='';
    }

    public function set_matk($data) {
        $this->matk = $data;
    }

    public function set_tentk($data) {
        $this->tentk = $data;
    }
    
    public function set_tenkh($data) {
        $this->tenkh = $data;
    }

    public function set_email($data) {
        $this->email = $data;
    }

    public function set_matkhau($data) {
        $this->matkhau = $data;
    }

    public function set_loaitk($data) {
        $this->loaitk = $data;
    }

    public function set_is_login($data) {
        $this->is_login = $data;
    }
    public function set_ngaysinh($data) {
        $this->ngaysinh = $data;
    }
    public function set_gioitinh($data) {
        $this->gioitinh = $data;
    }
	 public function set_diachi($data) {
        $this->diachi = $data;
    }
//get funtion
    public function get_matk() {
        return $this->matk;
    }

    public function get_tentk() {
        return $this->tentk;
    }
    
    public function get_tenkh() {
        return $this->tenkh;
    }

    public function get_email() {
        return $this->email;
    }

    public function get_matkhau() {
        return $this->matkhau;
    }

    public function get_loaitk() {
        return $this->loaitk;
    }

    public function get_is_login() {
        return $this->is_login;
    }
    public function get_ngaysinh() {
        return $this->ngaysinh;
    }
    public function get_gioitinh() {
        return $this->gioitinh;
    }
	public function get_diachi() {
        return $this->diachi;
    }
//////////////////////////
    public function Save() {
        $data = array(
		"matk" => $this->matk,
		"tentk" => $this->tentk,
		"email" => $this->email, 
		"matkhau" => $this->matkhau,
		"loaitk" => $this->loaitk,
		"is_login"=>  $this->is_login,
		"ngaysinh"=>  $this->ngaysinh,
		"gioitinh"=>  $this->gioitinh,
		"tenkh"=>  $this->tenkh,
		"diachi"=>$this->diachi);
        $this->db->insert("taikhoan", $data);
    }

    public function UpdateField($strWhere) {
        $arr = array();
        $arr["matk"] = $this->matk;
        $arr["tentk"] = $this->tentk;
        $arr["matkhau"] = $this->matkhau;
        $arr["email"] = $this->email;
        $arr["loaitk"] = $this->loaitk;
        $arr["is_login"] = $this->is_login;
        $arr["ngaysinh"] = $this->ngaysinh;
        $arr["gioitinh"] = $this->gioitinh;
        $arr["tenkh"] = $this->tenkh;
		$arr["diachi"]=$this->diachi;
        if (!empty($strWhere)) {
            $this->db->where($strWhere,NULL,TRUE);
        }
        $this->db->update("taikhoan", $arr);
    }

    public function Delete($strWhere = NULL) {
        if (!is_null($strWhere)) {
            $this->db->where($strWhere, NULL, FALSE);
            $this->db->delete("taikhoan");
        }
        else
            $this->db->empty_table('taikhoan');
    }
    
    public function getObject($strWhere,$strValue) {
        //$this->Oncreate();
        if($strWhere!=NULL)
        $this->db->where($strWhere,$strValue);
         $this->db->from("taikhoan");
        // $this->db->join('loaitk', 'taikhoan.loaitk=loaitaikhoan.loaitk');
         $query =  $this->db->get();
        $ar = $query->row_array();
        if (!empty($ar)) {
            $this->matk = $ar["matk"];
            $this->tentk = $ar["tentk"];
            $this->matkhau = $ar["matkhau"];
            $this->email = $ar["email"];
             $this->loaitk = $ar["loaitk"];
            $this->is_login = $ar["is_login"];
            $this->ngaysinh = $ar["ngaysinh"];
            $this->gioitinh=$ar['gioitinh'];
			$this->diachi=$ar['diachi'];
        }
        return $ar;
    }
    public function dangnhap($tentk, $pass) {
        $bool = false;
        $this->db->select("*");
        $this->db->where("tentk", $tentk);
        $this->db->where("matkhau", $pass);
        $this->db->from("taikhoan");
        //$this->db->get()->result();
        //echo($this->db->last_query());
        $ar = $this->db->get()->row_array();
        if (!empty($ar)) {
            $bool = TRUE;
            $this->matk = $ar["matk"];
            $this->tenkh =$ar["tenkh"];
            $this->tentk = $ar["tentk"];
            $this->matkhau = $ar["matkhau"];
            $this->email = $ar["email"];
            $this->loaitk = $ar["loaitk"];
            $this->is_login = $bool;
            $this->ngaysinh = $ar["ngaysinh"];
            $this->gioitinh = $ar["gioitinh"];
			$this->diachi=$ar["diachi"];
            $data = array("tentk" => $this->tentk,"matk"=>$this->matk);
            $this->UpdateField($data);
            $this->session->set_userdata($data);
        }
        return $bool;
    }

    public function logout() {
        
    }

    public function get_top_id() {
        $Query = $this->db->query("select * from taikhoan");
        $dem = $Query->num_rows();
        return ($dem);
    }

}
?>

